/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.symbiosys.workbench.tests.contexts;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.contexts.IContextService;
import org.eclipse.ui.tests.harness.util.CallHistory;

import org.reliablesource.client.ui.viewers.IReliableViewer;
import org.reliablesource.client.ui.workbench.views.ReliableViewPart;

import org.reliablesource.symbiosys.ui.tests.viewers.MockSelectorViewer;


/**
 *
 * ContextViewPart.java<br>
 * Created on: Jun 27, 2008<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class ContextViewPart extends ReliableViewPart {
    /**
     *
     */
    public static final String CONTEXT_ID = "org.reliablesource.symbiosys.workbench.tests.contexts";
    public static final String ID = CONTEXT_ID + ".contextpart";
    public static String NAME = "Non Specific Context Mock View Part";
    CallHistory callTrace;

    public ContextViewPart() {
        super();
        callTrace = new CallHistory(this);
    }

    @Override
    public void init(IViewSite _site) throws PartInitException {
        super.init(_site);
        callTrace.add("init");
        addContext();
    }

    @Override
    public void init(IViewSite site, IMemento memento)
        throws PartInitException {
        super.init(site, memento);
        callTrace.add("init");
        addContext();
    }

    private void addContext() throws PartInitException {
        IContextService contextService = (IContextService) getSite()
                                                               .getService(IContextService.class);

        if (!contextService.getContext(CONTEXT_ID).isDefined()) {
            throw new PartInitException("Failed to find context " + CONTEXT_ID);
        }

        contextService.activateContext(CONTEXT_ID);
    }

    public void createPartControl(Composite parent) {
        super.createPartControl(parent);
        callTrace.add("createPartControl");
    }

    public void dispose() {
        super.dispose();
        callTrace.add("dispose");
        System.err.println("Disposing view:" + ID);
        callTrace.printToConsole();
    }

    public void setFocus() {
        super.setFocus();
        callTrace.add("setFocus");
    }

    /* (non-Javadoc)
     * @see org.reliablesource.client.ui.workbench.views.ReliableViewPart#createViewer()
     */
    @Override
    protected IReliableViewer createViewer() {
        return new MockSelectorViewer();
    }

    /* (non-Javadoc)
     * @see org.reliablesource.client.ui.workbench.views.IReliableViewPart#getPartId()
     */
    public String getPartId() {
        return ID;
    }
}
